package com.ruoyi.data.service.impl;

import com.ruoyi.base.domain.BaseGood;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MyUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.data.domain.DataSale;
import com.ruoyi.data.domain.DataSaleItem;
import com.ruoyi.data.mapper.DataSaleMapper;
import com.ruoyi.data.service.IDataSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单信息Service业务层处理
 *
 * @author zzp
 * @date 2022-10-03
 */
@Service
public class DataSaleServiceImpl implements IDataSaleService {
    @Autowired
    private DataSaleMapper dataSaleMapper;

    /**
     * 查询订单信息
     *
     * @param id 订单信息主键
     * @return 订单信息
     */
    @Override
    public DataSale selectDataSaleById(Long id) {
        return dataSaleMapper.selectDataSaleById(id);
    }

    /**
     * 查询订单信息列表
     *
     * @param dataSale 订单信息
     * @return 订单信息
     */
    @Override
    public List<DataSale> selectDataSaleList(DataSale dataSale) {
        return dataSaleMapper.selectDataSaleList(dataSale);
    }

    /**
     * 新增订单信息
     *
     * @param dataSale 订单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDataSale(DataSale dataSale) {
        dataSale.setBillNumber(MyUtil.getBillNumber());
        dataSale.setCreateTime(DateUtils.getNowDate());
        int rows = dataSaleMapper.insertDataSale(dataSale);
        insertDataSaleItem(dataSale);
        return rows;
    }

    /**
     * 修改订单信息
     *
     * @param dataSale 订单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDataSale(DataSale dataSale) {
        dataSale.setUpdateTime(DateUtils.getNowDate());
        dataSaleMapper.deleteDataSaleItemBySaleId(dataSale.getId());
        int row = dataSaleMapper.updateDataSale(dataSale);
        insertDataSaleItem(dataSale);
        return row;
    }

    /**
     * 批量删除订单信息
     *
     * @param ids 需要删除的订单信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDataSaleByIds(Long[] ids) {
        dataSaleMapper.deleteDataSaleItemBySaleIds(ids);
        return dataSaleMapper.deleteDataSaleByIds(ids);
    }

    /**
     * 删除订单信息信息
     *
     * @param id 订单信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDataSaleById(Long id) {
        dataSaleMapper.deleteDataSaleItemBySaleId(id);
        return dataSaleMapper.deleteDataSaleById(id);
    }

    @Override
    public int updateDataSaleExam(DataSale dataSale) {
        return dataSaleMapper.updateDataSale(dataSale);
    }

    @Override
    public int updateDataSaleCancel(DataSale dataSale) {
        return dataSaleMapper.updateDataSaleCancel(dataSale);
    }

    @Override
    public int updateDataSaleSync(BaseGood baseGood) {
        return dataSaleMapper.updateDataSaleSync(baseGood);
    }

    /**
     * 新增订单明细信息
     *
     * @param dataSale 订单信息对象
     */
    public void insertDataSaleItem(DataSale dataSale) {
        List<DataSaleItem> dataSaleItemList = dataSale.getDataSaleItemList();
        Long id = dataSale.getId();
        Integer goldScale = dataSale.getGoldScale();
        Integer allGold = 0;
        Integer addUser;
        Integer allAmount = 0;
        if (StringUtils.isNotNull(dataSaleItemList)) {
            List<DataSaleItem> list = new ArrayList<>();
            for (DataSaleItem dataSaleItem : dataSaleItemList) {
                allGold += dataSaleItem.getGoldAmount();
                allAmount += dataSaleItem.getGoodAmount();
                dataSaleItem.setSaleId(id);
                dataSaleItem.setCreateBy(dataSale.getCreateBy());
                dataSaleItem.setCreateTime(DateUtils.getNowDate());
                list.add(dataSaleItem);
            }
            if (list.size() > 0) {
                addUser = allGold / goldScale;
                dataSale.setAllGold(allGold);
                dataSale.setAddUser(addUser);
                dataSale.setAllAmount(allAmount);
                dataSaleMapper.updateDataSale(dataSale);
                dataSaleMapper.batchDataSaleItem(list);
            } else {
                dataSale.setAllGold(0);
                dataSale.setAddUser(0);
                dataSale.setAllAmount(0);
                dataSaleMapper.updateDataSale(dataSale);
            }
        }
    }
}
